package com.police.view;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.police.R;
import com.police.activity.MainActivity;
import com.police.model.EventBean;

/**
 * 事件弹出框
 * 
 * @author yinxing
 * 
 */
public class EventPopupwindow extends LinearLayout {

	MainActivity main;
	ListView eventList;
	public LinearLayout layout;
	LayoutInflater inflater;
	LayoutParams layoutParams;
	ArrayList<EventBean> events = new ArrayList<EventBean>();
	public EventAdapter adapter;

	public EventPopupwindow(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		main = (MainActivity) context;
		initView();
	}

	private void initView() {
		inflater = LayoutInflater.from(main);
		layout = (LinearLayout) inflater.inflate(R.layout.event_view, null);
		layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
		eventList = (ListView) layout.findViewById(R.id.event_list);
		this.addView(layout, layoutParams);
		adapter = new EventAdapter();
		if (adapter != null) {
			eventList.setAdapter(adapter);
		}
	}

	private class EventAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return events.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			EventBean bean = null;
			bean = events.get(position);
			if (bean == null) {
				return null;
			}
			ViewHolder holder;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.event_item, null);
				holder = new ViewHolder();
				holder.eventId = (TextView) convertView
						.findViewById(R.id.event_id);
				holder.eventTitle = (TextView) convertView
						.findViewById(R.id.event);
				holder.eventTime = (TextView) convertView
						.findViewById(R.id.time);
				convertView.setTag(holder);
			}
			holder = (ViewHolder) convertView.getTag();
			holder.eventId.setText(bean.eventId);
			holder.eventTitle.setText(bean.eventTitle);
			holder.eventTime.setText(bean.eventTime);
			return convertView;
		}

	}

	static class ViewHolder {

		public TextView eventId;
		public TextView eventTitle;
		public TextView eventTime;
	}
}
